package com.huangc.cn.rainbow.asyc;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

/**
 * @Author:冯超云
 * @Date:2021/4/29 15:52
 * @Desc:
 **/
@Component
@Async
public class As {

    //下面是3个异步任务（不带返回值的）
    public void task1() throws InterruptedException {
        long begin=System.currentTimeMillis();
        Thread.sleep(3000L);
        long end=System.currentTimeMillis();
        System.out.println("任务1耗时"+(end-begin));
    }

    public void task2() throws InterruptedException {
        long begin=System.currentTimeMillis();
        Thread.sleep(2000L);
        long end=System.currentTimeMillis();
        System.out.println("任务2耗时"+(end-begin));
    }

    public void task3() throws InterruptedException {
        long begin=System.currentTimeMillis();
        Thread.sleep(1000L);
        long end=System.currentTimeMillis();
        System.out.println("任务3耗时"+(end-begin));
    }

    //下面是3个异步任务（带返回值的，可以在调用出取地返回值）
    public Future<String> task4() throws InterruptedException {
        long begin=System.currentTimeMillis();
        Thread.sleep(3000L);
        long end=System.currentTimeMillis();
        return new AsyncResult<String>("任务4耗时"+(end-begin));
    }

    public Future<String> task5() throws InterruptedException {
        long begin=System.currentTimeMillis();
        Thread.sleep(2000L);
        long end=System.currentTimeMillis();
        return new AsyncResult<String>("任务5耗时"+(end-begin));
    }

    public Future<String> task6() throws InterruptedException {
        long begin=System.currentTimeMillis();
        Thread.sleep(1000L);
        long end=System.currentTimeMillis();
        return new AsyncResult<String>("任务6耗时"+(end-begin));
    }
}
